package com.powernode.service;

import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.powernode.config.UploadProperties;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 描述：
 * 作者：动力节点·武汉
 */
@Component
@Log4j2
@EnableConfigurationProperties(value = {UploadProperties.class})
public class UploadService {

    @Resource
    private UploadProperties properties;

    @Resource
    private FastFileStorageClient storageClient;

    /**
     * 上传图片
     * @param file
     * @return
     */
    public Map<String,Object> uploadImage(MultipartFile file) {
        // 1、校验文件类型
        String contentType = file.getContentType();
        if (!properties.getAllowTypes().contains(contentType)) {
            throw new RuntimeException("文件类型不支持");
        }
        // 2、校验文件内容
        try {
            BufferedImage image = ImageIO.read(file.getInputStream());
            if (image == null || image.getWidth() == 0 || image.getHeight() == 0) {
                throw new RuntimeException("上传文件有问题");
            }
        } catch (IOException e) {
            log.error("校验文件内容失败....{}", e);
            throw new RuntimeException("校验文件内容失败"+e.getMessage());
        }
        return this.uploadFile(file);

    }

    /**
     * 上传文件
     * @param file
     * @return
     */
    public Map<String,Object> uploadFile(MultipartFile file) {
        Map<String,Object> map=new HashMap<>();
        try {
            // 3、上传到FastDFS
            // 3.1、获取扩展名
            String extension = StringUtils.substringAfterLast(file.getOriginalFilename(), ".");
            // 3.2、上传
            StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(), extension, null);
            // 返回路径
            map.put("fileName",file.getOriginalFilename()); //文件名
            map.put("fullPath",properties.getBaseUrl() + storePath.getFullPath()); //全路径
            map.put("relativePath",storePath.getFullPath());//相对路径
            return map;
        } catch (IOException e) {
            log.error("【文件上传】上传文件失败！....{}", e);
            throw  new RuntimeException("【文件上传】上传文件失败！"+e.getMessage());
        }
    }

    /**
     * 从fastdfs里面删除文件
     * @param path group1/M00/00/00/rBNPwF9PZDqAaDeDAAF6kn4mN1Y027.jpg
     * @return
     */
    public Map<String,Object> deleteFile(String path) {
        Map<String,Object> res=new HashMap<>();
        try {
            // 删除
            storageClient.deleteFile(path);
            res.put("code",200);
            res.put("msg","删除成功");
            return res;
        } catch (Exception e) {
            log.error("【文件删除】删除文件失败！....{}", e);
            res.put("code",500);
            res.put("msg","删除文件失败");
            return res;
        }
    }
}

